package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.ListPopupWindow;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.ItemPopupGoodsBinding;
import com.cloudcreativity.blyg.databinding.LayoutDialogQueryGoodsBinding;
import com.cloudcreativity.blyg.sqlite.Goods;
import com.cloudcreativity.blyg.sqlite.utils.GoodsDaoUtils;

import java.util.List;

public class QueryGoodsDialog extends Dialog {

    private ListPopupWindow listPopupWindow;
    private GoodsDaoUtils goodsDaoUtils;
    private final LayoutDialogQueryGoodsBinding binding;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public QueryGoodsDialog(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        goodsDaoUtils = new GoodsDaoUtils(getContext());
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_query_goods,
                null,
                false);
        setContentView(binding.getRoot());
        binding.setUtils(this);
        assert getWindow()!= null;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        getWindow().getAttributes().width = metrics.widthPixels/2;
        binding.etKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(final Editable editable) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(TextUtils.isEmpty(editable)){
                            hidePopup();
                        }else{
                            List<Goods> goods = goodsDaoUtils.queryByKey(editable.toString());
                            if(goods!=null && !goods.isEmpty())
                                showPopup(goods);
                        }
                    }
                },300);

            }
        });
    }

    private void showPopup(final List<Goods> goods){
        if(listPopupWindow==null){
            listPopupWindow = new ListPopupWindow(getContext());
            listPopupWindow.setAnchorView(binding.etKey);
            listPopupWindow.setVerticalOffset(10);
            listPopupWindow.setWidth(750);
        }
        listPopupWindow.setAdapter(new MyGoodsAdapter(goods,getContext()));
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try{
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(goods.get(i));
                    }
                }catch (Exception e){
                    ToastUtils.showShortToast(getContext(),"选择失败");
                }finally {
                    close();
                }

            }
        });
        listPopupWindow.show();
    }

    private void close(){
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    private void hidePopup(){
        if(listPopupWindow!=null && listPopupWindow.isShowing()){
            listPopupWindow.dismiss();
        }
    }

    public interface OnItemClickListener{
        void onItemClick(Goods goods);
    }


    private static class MyGoodsAdapter extends BaseAdapter {

        private List<Goods> goods;
        private Context context;
        MyGoodsAdapter(List<Goods> goods,Context context) {
            this.goods = goods;
            this.context = context;
        }

        @Override
        public int getCount() {
            return goods==null?0:goods.size();
        }

        @Override
        public Object getItem(int i) {
            return goods.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null){
                ItemPopupGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                        R.layout.item_popup_goods,
                        null,
                        false);
                view = binding.getRoot();
                view.setTag(binding);
            }
            ItemPopupGoodsBinding binding = (ItemPopupGoodsBinding) view.getTag();
            binding.setItem(goods.get(i));
            return view;
        }
    }
}
